#include "plconfig.h"
#include "plcan_config.h"
#include "plcan.h"
//#include "pluds_config.h"
//#include "plisotp.h"
#include "cocan.h"


uint8 canrx_config_index_85;
uint8 canrx_EMSSSMStatus;
uint8 canrx_EMS_len = 0;



uint8 cantx_config_index_145;
uint8 cantx_EGSMStatusAliveCounter = 0;



bool plcan_extract_signal_EMS_85(PlcanDataBuffer *data_buffer, void *arguments) {

	(void)arguments; /*not used!!*/

	canrx_EMS_len = data_buffer->dlc;
	canrx_EMSSSMStatus = (uint8)((data_buffer->data[5] & 0x07U));
	return TRUE;
}


bool plcan_fill_signal_145(PlcanDataBuffer *buffer, void *arguments) {

	uint8 index;
	(void)arguments; /*not used!!*/

	buffer->data[0] = 0x00U;
	buffer->data[1] = 0x00U;
	buffer->data[2] = 0x00U;
	buffer->data[3] = 0x00U;
	buffer->data[4] = 0x00U;
	buffer->data[5] = 0x00U;
	buffer->data[6] = 0x00U;
	buffer->data[7] = 0x00U;

	return TRUE;
}

void plcan_tx_145_confirm(uint8 config_index, bool transmit_result){

    if(cantx_EGSMStatusAliveCounter < 15u){
    	cantx_EGSMStatusAliveCounter++;
    }else{
    	cantx_EGSMStatusAliveCounter = 0;
    }
}

const PlcanRxidDef plcan_rx_defs[] = {
	/*canid                     	    rx_can_group 		pre_checker 	pre_handler 					pre_handler_arguments 	rx_callback                 	tmeout_cycle	timeout_callback	config_index_ref*/
	{0x733u,					    	CAN_GRP_USER0,		NULL,		plcan_extract_signal_EMS_85,		    NULL,					NULL,							15U,		  	NULL,				&canrx_config_index_85  									},

#if PLCFG_CCP_ENABLED == 1
	{CCP_RX_CANID,              		CAN_GRP_NONE,       NULL,		NULL,								NULL,					plccp_rx_callback,       		0U,           	NULL,             	&plccp_rx_config_index },
#endif

#if CANNM_DEFAULT_ENABLED == 1
	{0x400u,							CAN_GRP_NW_MANAGER,	NULL,		NULL,								NULL,					CanNm_RxIndication,				0U,			  	NULL,				&plcannm_rx_config_index  									},
#endif

#if UDS_DEFAULT_ENABLED == 1
	{UDS_DEFAULT_PHYREQ_RXID,   		CAN_GRP_DIAG,       NULL,		NULL,								NULL,					plisotp_physical_indication,	0U,			  	NULL,		     	&(plisotp_canrx_physical_handles[ISOTP_DEFAULT_CHANNEL])   	},
	{UDS_DEFAULT_FUNREQ_RXID,   		CAN_GRP_DIAG,    	NULL,		NULL,								NULL,					plisotp_functional_indication,	0U,			  	NULL,			 	&(plisotp_canrx_functional_handles[ISOTP_DEFAULT_CHANNEL]) 	},
#endif

};

const uint8 plcan_rx_canid_count = array_length(plcan_rx_defs);
PlcanRxInfo plcan_rx_infos[array_length(plcan_rx_defs)];


const PlcanTxidDef plcan_tx_defs[] = {
	/* canid                 tx_can_group       dlc             transmit_period_ms      transmit_offset_ms  fill_handler                fill_handler_arguments       post_filler          tx_callback                         config_index_ref*/
	{0x145u,				 CAN_GRP_USER0,	   0x08U,			20U,					0U,					plcan_fill_signal_145,  	NULL,						NULL,	              plcan_tx_145_confirm,					&cantx_config_index_145						},
	//{0x146u,				 CAN_GRP_USER0,	   0x08U,			10U,					0U,					plcan_fill_signal_146,  	NULL,						NULL,                 plcan_tx_146_confirm,					&cantx_config_index_145						},

#if PLCFG_CCP_ENABLED == 1
	{CCP_TX_CANID,           CAN_GRP_NONE,      0x08U,			0U,						0U,					NULL,						NULL,						NULL,								NULL,									&plccp_tx_config_index							},
#endif

#if CANNM_DEFAULT_ENABLED == 1
	{0x40Au,				 CAN_GRP_NW_MANAGER,0x08U,			0U,						0U,					NULL,						NULL,						NULL,								CanNm_TxConfirmation,					&plcannm_cantx_handle							},
#endif

#if UDS_DEFAULT_ENABLED == 1
	{UDS_DEFAULT_RES_TXID,	 CAN_GRP_DIAG,	   0x08U,		  	0U,  					0U,				 	NULL,						NULL,						NULL,								plisotp_tx_confirm,						&(plisotp_cantx_handles[ISOTP_DEFAULT_CHANNEL])	},
#endif
};

const uint8 plcan_tx_canid_count = array_length(plcan_tx_defs);
PlcanTxInfo plcan_tx_infos[array_length(plcan_tx_defs)];

